import {BaseData, MainData, PK, pkType, StatusData} from "../../coreModule/data/BaseData";
import {DataOccasion, field, occasion} from "../../coreModule/data/DataLoader";
import {userMgr} from "../../userModule/managers/UserManager";
import {Bill, PayType} from "./Bill";
import {DateUtils} from "../../../utils/DateUtils";

export enum TradeStatus {
	NotPaid = -1, Paid, Refunding, Refunded, Closed
}

export enum TradeBusiness {
	Exam, Task
}

export type TradeAttach = {
	type?: TradeBusiness
	params?: any[]
}

export class Trade extends StatusData<TradeStatus> {

	@field(pkType())
	public payerUserId?: PK; // 用户，为空表示系统
	@field(pkType())
	public payeeUserId?: PK; // 商家，为空表示系统
	@field
	public price: number = 0;

	@field(pkType())
	public lastBillId: PK;
	@field(Number)
	public payType: PayType;

	@field
	public description: string = "";
	@field(Object)
	public attach: TradeAttach = {};

	@field(Number)
	public createdAt: number;

	@field(Number)
	public maxPayTime: number; // 最迟的支付时间
	@field(Number)
	public delayFeeRate: number = 0; // 延误费率（每天为单位）

	@field
	public status: TradeStatus = TradeStatus.NotPaid;

	// region 额外信息

	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isPayer: boolean
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isPayee: boolean

	@field(String)
	@occasion(DataOccasion.Extra)
	priceStr: string

	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isPaid: boolean

	@field(String)
	@occasion(DataOccasion.Extra)
	typeStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	payTypeStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	statusStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	createTimeStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	successTimeStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	maxPayTimeStr: string

	public refresh() {
		this.isPayer = this.payerUserId == userMgr().openid;
		// this.isDeposit = this.type == TradeType.Deposit;
		this.priceStr = (this.price / 100).toFixed(2);

		switch (this.payType) {
			case PayType.WeChat: this.payTypeStr = "微信支付"; break;
			case PayType.Balance: this.payTypeStr = "余额"; break;
		}

		// this.typeStr = this.isDeposit ? "押金支付" : "普通交易";
		this.typeStr = this.isPayer ? "支出" : "收入";
		this.statusStr = this.getStatusStr();

		this.isPaid = this.status == TradeStatus.Paid;

		this.createTimeStr = DateUtils.time2Str(this.createdAt);
		this.maxPayTimeStr = DateUtils.time2Str(this.maxPayTime);

		if (this.isPaid)
			this.successTimeStr = DateUtils.time2Str(this.statusTime);
	}
	private getStatusStr() {
		switch (this.status) {
			case TradeStatus.Paid: return "已支付";
			case TradeStatus.NotPaid: return "未支付";
			case TradeStatus.Closed: return "已关闭";
			case TradeStatus.Refunding: return "退款中";
			case TradeStatus.Refunded: return "已退款";
			default: return "异常";
		}
	}

	// endregion

}

export class TradeIndexData extends BaseData {

	@field(pkType())
	public _id: PK;
	@field(pkType())
	public payerUserId?: PK; // 用户，为空表示系统
	@field(pkType())
	public payeeUserId?: PK; // 商家，为空表示系统
	@field
	public price: number = 0;
	@field
	public description: string = "";
	@field(Object)
	public attach: TradeAttach = {};
	@field(Number)
	public createdAt: number;
	@field
	public status: TradeStatus = TradeStatus.NotPaid;

	// region 额外信息

	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isPayer: boolean
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isPayee: boolean
	@field(String)
	@occasion(DataOccasion.Extra)
	priceStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	statusStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	typeStr: string
	@field(String)
	@occasion(DataOccasion.Extra)
	payTypeStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	timeStr: string

	public refresh() {
		this.isPayer = this.payerUserId === userMgr().openid;
		this.isPayee = this.payeeUserId === userMgr().openid;
		this.priceStr = (this.price / 100).toFixed(2);
		this.payTypeStr = this.isPayer ? "支出" : "收入";
		this.statusStr = this.getStatusStr();

		this.timeStr = DateUtils.time2Str(this.createdAt);
	}
	private getStatusStr() {
		switch (this.status) {
			case TradeStatus.Paid: return "已支付";
			case TradeStatus.NotPaid: return "未支付";
			case TradeStatus.Closed: return "已关闭";
			case TradeStatus.Refunding: return "退款中";
			case TradeStatus.Refunded: return "已退款";
			default: return "异常";
		}
	}

	// endregion

	/**
	 * 测试数据
	 */
	public static testData(){
		const res1 = new TradeIndexData();
		res1._id = "123456789";
		res1.status = TradeStatus.Paid;
		res1.description = "支付押金";
		res1.createdAt = Date.now();
		res1.price = 348;

		const res2 = new TradeIndexData();
		res2._id = "123456780";
		res2.status = TradeStatus.NotPaid;
		res2.description = "待支付押金";
		res2.createdAt = Date.now();
		res2.price = 50;

		return [res1, res2];
	}
}
